package com.example.shinelon.gobang;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.shinelon.gobang.MyActivity.Map;
import com.example.shinelon.gobang.MyView.GoBangView;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import cn.jpush.android.api.JPushInterface;

/**
 * keytool -list -v -keystore C:\Users\Shinelon\.android\debug.keystore
 * 微信:35541a6fc2d438317aab699a928f76cd
 * 微信:AppID：wxa10e6d974735ab5b
 * SHA1: 51:11:AD:D5:1D:99:C6:F1:64:66:EA:2F:9A:05:BC:8A:9B:7D:E5:FA
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoBangView.onWinChanged {
    /**
     * 分享按钮
     */
    private Button mShare;
    private Button mButRestart;
    private Button mButMap;
    private Button mButOpen;
    private GoBangView mGoBangView;
    private Button mButUndo;
    private Button mButNextSetp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        JPushInterface.init(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        mShare.setOnClickListener(this);
        mButRestart.setOnClickListener(this);
        mGoBangView.setOnWinChanged(this);
        mButMap.setOnClickListener(this);
        mButOpen.setOnClickListener(this);
        mButUndo.setOnClickListener(this);
        mButNextSetp.setOnClickListener(this);
        mWXapi.registerApp(APP_ID_WEIXIN);

    }

    private void initView() {
        mShare = (Button) findViewById(R.id.but_share);
        mButRestart = (Button) findViewById(R.id.but_restart);
        mGoBangView = (GoBangView) findViewById(R.id.main_goBang);
        mButMap = (Button) findViewById(R.id.but_map);
        mButOpen = (Button) findViewById(R.id.but_open);
        mButUndo = (Button) findViewById(R.id.but_undo);
        mButNextSetp = (Button) findViewById(R.id.but_nextStep);
        mWXapi = WXAPIFactory.createWXAPI(this, APP_ID_WEIXIN);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_share:
                share();
                break;
            case R.id.but_restart:
                restart();
                break;
            case R.id.but_map:
                Intent intent = new Intent(MainActivity.this, Map.class);
                startActivity(intent);
                overridePendingTransition(R.anim.start, R.anim.end);
                break;
            case R.id.but_open:
                openWeiXin();
                break;
            case R.id.but_undo:
                undo();
                break;
            case R.id.but_nextStep:
                nextStep();
                break;
        }
    }

    private void nextStep() {
        if (!mGoBangView.nextStep()) {
            Toast.makeText(MainActivity.this, "已经是最开始的了", Toast.LENGTH_SHORT).show();
        }
    }

    private void undo() {
        if (!mGoBangView.undo()) {
            Toast.makeText(MainActivity.this, "不能再返回了", Toast.LENGTH_SHORT).show();
        }
    }

    private void share() {
        Toast.makeText(MainActivity.this, "打开微信中,请稍等...", Toast.LENGTH_SHORT).show();
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = "http://www.baidu.com";
        WXMediaMessage message = new WXMediaMessage(webpage);
        message.title = "下载";
        message.description = "欢迎下载五子棋";

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");

        req.message = message;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        mWXapi.sendReq(req);
    }

    /**
     * 根据传入的key生成唯一的标识符
     *
     * @param key
     * @return
     */
    private String buildTransaction(String key) {

        return String.valueOf(key + System.currentTimeMillis());
    }

    /**
     * 微信的APP_ID
     */
    private static final String APP_ID_WEIXIN = "wxa10e6d974735ab5b";
    private IWXAPI mWXapi;

    private void openWeiXin() {
        mWXapi.openWXApp();
    }

    private void restart() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("重新开始");
        builder.setMessage("确认清空棋盘,重新开始?");
        builder.setPositiveButton("取消", null);
        builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mGoBangView.restart();
            }
        });
        builder.show();
    }

    @Override
    public void winChanged(boolean isOneWin) {
        if (isOneWin) {
            Toast.makeText(MainActivity.this, "先手获胜", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "后手获胜", Toast.LENGTH_SHORT).show();
        }
    }
}
